// pages/leftHos/leftHos.js
const lbbService = require('../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectorArray: [0, 0, 0],
    allChoosed: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCitys()
    this.getAllHos()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  getCitys: function () {
    let that = this
    lbbService.getCitys({}, function (res) {
      that.setData({
        propArray: res
      })
      that.getHospitalLeft()
    })
  },
  getAllHos: function(){
    let that = this
    lbbService.getHospitalLeft({ array: [0,0,0] }, function (res) {
      that.setData({
        all_hos: res.data
      })
    })
  },
  getHospitalLeft: function () {
    let that = this
    let selectorArray = that.data.selectorArray
    lbbService.getHospitalLeft({ array: selectorArray }, function (res) {
      that.setData({
        hospitalList: res.data
      })
      let hospitalList = that.data.hospitalList
      hospitalList.forEach(item => {
        item.checked = that.data.allChoosed
      })
      that.setData({
        hospitalList: hospitalList
      })
      let choosedHosList = []
      if (that.data.allChoosed) {
        hospitalList.forEach(item => {
          let item1 = {}
          item1.id = item.id
          item1.hospitalName = item.hospitalName
          choosedHosList.push(item1)
        })
      }
      that.setData({
        choosedHosList: choosedHosList
      })
    })
  },
  searchEventListener: function (e) {
    let that = this
    console.log('searchEventListener>>', e)
    var inputName = ''
    if (!!e) {
      inputName = e.detail
      that.setData({
        inputName: inputName
      })
    } else {
      inputName = that.data.inputName
    }
    console.log('inputName', inputName)
    if (!!inputName) {
      let hospitalList = that.data.all_hos.filter(item => item.hospitalName.indexOf(inputName) != -1)
      that.setData({
        hospitalList: hospitalList
      })
    }
  },
  //事件处理函数
  myEventListener: function (e) {
    let that = this
    console.log("myEventListener>>", e)
    that.setData({
      selectorArray: e.detail
    })
    that.getHospitalLeft()
  },
  checkChange: function(e){
    console.log(e)
    let that = this
    let idArray = e.detail.value
    let hospitalList = that.data.hospitalList
    let choosedHosList = []
    console.log('idArray', idArray)
    hospitalList.forEach(item => {
      if (idArray.indexOf(item.id + '') != -1){
        console.log('>>>>')
        let item1 = {}
        item1.id = item.id
        item1.hospitalName = item.hospitalName
        choosedHosList.push(item1)
      }
    })
    that.setData({
      choosedHosList: choosedHosList
    })
    if(that.data.choosedHosList.length==hospitalList.length){
      that.setData({
        allChoosed: true
      })
    }else {
      that.setData({
        allChoosed: false
      })
    }
  },
  radioChange: function(){
    let that = this
    that.setData({
      allChoosed: !that.data.allChoosed
    })
    let hospitalList = that.data.hospitalList
    hospitalList.forEach(item => {
      item.checked = that.data.allChoosed
    })
    that.setData({
      hospitalList: hospitalList
    })
    let choosedHosList = []
    if(that.data.allChoosed){
      hospitalList.forEach(item => {
        let item1 = {}
        item1.id = item.id
        item1.hospitalName = item.hospitalName
        choosedHosList.push(item1)
      })
    }
    that.setData({
      choosedHosList: choosedHosList
    })
  },
  submit: function(){
    let that = this
    let pages = getCurrentPages()
    let prevPage = pages[pages.length - 2]
    let hosList = that.data.choosedHosList
    prevPage.setData({
      addList: hosList
    })
    wx.navigateBack({
      delta: 1
    })
  }
})